package org.example.ui.customer;

import org.example.business.LoginBusiness;
import org.example.business.UserBusiness;
import org.example.manage.UserManage;
import org.example.model.MilkTea;
import org.example.model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PersonalPanel extends JPanel {
    private JLabel[] label;
    private JLabel imageLabel;
    private final JButton editButton, preserveButton;
    private final JTextField[] textFields;
    private SpringLayout layout;
    private JButton back = new JButton("返回");
    private boolean isEdit = false;
    private String avatarPath = UserBusiness.user.avatarPath;
    public PersonalPanel(){
        label = new JLabel[6];
        Image image = new ImageIcon("data/" + UserBusiness.user.avatarPath).getImage()
                .getScaledInstance(100,100,Image.SCALE_DEFAULT);
        textFields = new JTextField[5];
        imageLabel = new JLabel();
        layout = new SpringLayout();
        setLayout(layout);
        editButton = new JButton("编辑");
        preserveButton = new JButton("保存");
        imageLabel.setIcon(new ImageIcon(image));
        String[] array = {"头像：", "昵称：", "邮箱：", "密码：", "个性签名：", "身份："},
                array1 = {UserBusiness.user.name, UserBusiness.user.mailbox, "********", UserBusiness.user.signature, UserBusiness.user.identity};
        for(int i = 0; i<6; i++){
            label[i] = new JLabel(array[i]);
            label[i].setFont(new Font("",Font.BOLD,25));
            label[i].setBorder(BorderFactory.createEtchedBorder());
            add(label[i]);
            if(i==0){
                layout.putConstraint(SpringLayout.WEST, label[i],-150, SpringLayout.HORIZONTAL_CENTER,this);
                layout.putConstraint(SpringLayout.NORTH, label[i],150, SpringLayout.NORTH,this);
            }
            else if(i==1){
                layout.putConstraint(SpringLayout.WEST, label[i],-150, SpringLayout.HORIZONTAL_CENTER,this);
                layout.putConstraint(SpringLayout.NORTH, label[i],70, SpringLayout.SOUTH,label[i-1]);
            }
            else{
                layout.putConstraint(SpringLayout.WEST, label[i],-150, SpringLayout.HORIZONTAL_CENTER,this);
                layout.putConstraint(SpringLayout.NORTH, label[i],30, SpringLayout.SOUTH, label[i-1]);
            }
        }
        editButton.setFont(new Font("", Font.BOLD,25));
        preserveButton.setFont(new Font("", Font.BOLD,25));
        back.setFont(new Font("", Font.BOLD,20));
        add(imageLabel);
        add(editButton);
        add(preserveButton);
        add(back);
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                LoginBusiness.layout.show(LoginBusiness.panel, "1");
            }
        });
        for (int i = 0; i < 5; i++) {
            textFields[i] = new JTextField(20);
            textFields[i].setText(array1[i]);
            textFields[i].setBorder(null);
            textFields[i].setBackground(null);
            textFields[i].setFocusable(false);
            textFields[i].setFont(new Font("", Font.BOLD,25));
            textFields[i].setBorder(BorderFactory.createEtchedBorder());
            add(textFields[i]);
            if(i==0){
                layout.putConstraint(SpringLayout.NORTH, imageLabel,110, SpringLayout.NORTH,this);
                layout.putConstraint(SpringLayout.WEST, imageLabel,50, SpringLayout.EAST, label[i]);
                layout.putConstraint(SpringLayout.WEST, textFields[i],40, SpringLayout.EAST, label[i]);
                layout.putConstraint(SpringLayout.NORTH, textFields[i],45, SpringLayout.SOUTH, imageLabel);
            }
            else{
                layout.putConstraint(SpringLayout.WEST, textFields[i],40, SpringLayout.EAST, label[i+1]);
                layout.putConstraint(SpringLayout.NORTH, textFields[i],30, SpringLayout.SOUTH, textFields[i-1]);
            }
            layout.putConstraint(SpringLayout.EAST, textFields[i], 400, SpringLayout.EAST, label[1]);
        }
        editButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textFields[0].setFocusable(true);
                textFields[3].setFocusable(true);
                textFields[0].setBackground(Color.CYAN);
                textFields[3].setBackground(Color.CYAN);
                isEdit = true;
            }
        });
        imageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isEdit) {
                    new User().savePhoto();
                    Image image1 = new ImageIcon("data/" + UserBusiness.user.avatarPath).getImage()
                            .getScaledInstance(100, 100, Image.SCALE_DEFAULT);
                    imageLabel.setIcon(new ImageIcon(image1));
                }
            }
        });
        preserveButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isEdit) {
                    UserManage manage = new UserManage();
                    manage.updateUser(String.format("name='%s'", textFields[0].getText()),
                            String.format("mailbox='%s'", UserBusiness.user.mailbox));
                    manage.updateUser(String.format("signature='%s'", textFields[3].getText()),
                            String.format("mailbox='%s'", UserBusiness.user.mailbox));
                    UserBusiness.user = manage.selectUser(textFields[1].getText());
                    Image image1 = new ImageIcon("data/" + UserBusiness.user.avatarPath).getImage()
                            .getScaledInstance(30, 30, Image.SCALE_DEFAULT);
                    UserBusiness.item.setIcon(new ImageIcon(image1));
                    UserBusiness.item.setText(UserBusiness.user.name);
                    textFields[0].setFocusable(false);
                    textFields[3].setFocusable(false);
                    textFields[0].setBackground(null);
                    textFields[3].setBackground(null);
                    isEdit = false;
                    manage.close();
                    JOptionPane.showMessageDialog(PersonalPanel.this, "保存成功");
                }
            }
        });
        layout.putConstraint(SpringLayout.NORTH, back, 50, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, back, 50, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.WEST, editButton,-200, SpringLayout.HORIZONTAL_CENTER,this);
        layout.putConstraint(SpringLayout.NORTH, editButton,100, SpringLayout.SOUTH, label[5]);
        layout.putConstraint(SpringLayout.WEST, preserveButton,200, SpringLayout.HORIZONTAL_CENTER, editButton);
        layout.putConstraint(SpringLayout.NORTH, preserveButton,100, SpringLayout.SOUTH, label[5]);
    }
}
